package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.ReplaceBackgroundBean;
import com.formiaomiao.demain.util.UseGlideUtils;

import java.io.File;
import java.util.List;

public class ReplaceBackgroundAdapter extends RecyclerView.Adapter<ReplaceBackgroundAdapter.ViewHolder> {

    List<ReplaceBackgroundBean.DataBean> list;
    Context context;

    private int selectedPosition = -1;// 选中的位置

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    private onItemClickListener onItemClickListener;

    public interface onItemClickListener {
        void itemClickListener(View v, int i);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ReplaceBackgroundAdapter(List<ReplaceBackgroundBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                700);
        View convertView = LayoutInflater.from(context).inflate(R.layout.select_item_layout, viewGroup, false);
        convertView.setLayoutParams(param);
        ViewHolder holder = new ViewHolder(convertView);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int i) {
        holder.tv_theme.setVisibility(View.GONE);
        UseGlideUtils.setAddGlide(context, list.get(i).getImgurl(), holder.iv_theme);
        if (fileIsExists(Environment.getExternalStorageDirectory() + "/demain/chat/" +
                list.get(i).getImgname())) {
            holder.iv_none.setVisibility(View.GONE);
            list.get(i).setHavefile(true);
        } else {
            holder.iv_none.setVisibility(View.VISIBLE);
            list.get(i).setHavefile(false);
        }
        if (selectedPosition == i) {
            holder.iv_select.setVisibility(View.VISIBLE);
        } else {
            holder.iv_select.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.itemClickListener(v, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_theme, iv_select, iv_none;
        TextView tv_theme;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_theme = itemView.findViewById(R.id.iv_theme);
            iv_select = itemView.findViewById(R.id.iv_select);
            iv_none = itemView.findViewById(R.id.iv_none);
            tv_theme = itemView.findViewById(R.id.tv_theme);
        }
    }


    //判断文件是否存在
    private boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
